package org.example.carsystem.controller;

import com.alibaba.excel.EasyExcel;
import org.example.carsystem.config.Result;
import org.example.carsystem.config.UserListener;
import org.example.carsystem.domain.SysDept;
import org.example.carsystem.mapper.DeptMapper;
import org.example.carsystem.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;


    @GetMapping("/select")
    public Map<String,Object> deptSelect(@RequestParam(value = "deptName",required = false)String deptName,
                                         @RequestParam(value = "status",required = false)String status){
        List<SysDept> list = deptService.findAll(deptName,status);
        Map<String, Object> map = new HashMap<>();
        map.put("code",200);
        map.put("list",list);
        return map;
    }
    @GetMapping("/dept/tree")
    public Map<String,Object> deptTree(){
        return deptService.deptSelectAll();
    }

    @RequestMapping("sysUploud")
    public Result sysUploud(@RequestBody SysDept sysDept) {
        deptService.sysUploud(sysDept);
        return Result.ok("修改成功");
    }
    @RequestMapping("sysDelete")
    public Result sysDelete(Long deptId) {
        deptService.sysDelete(deptId);
        return Result.ok("删除成功");
    }

    @RequestMapping("/sysAdd")
    public Result sysAdd(@RequestBody SysDept sysDept) {
        deptService.sysinstr(sysDept);
        return Result.ok("添加成功");
    }


    @RequestMapping("excel")
    public void download(HttpServletResponse response,
                         @RequestParam(value = "deptName",required = false)String deptName,
                         @RequestParam(value = "status",required = false)String status) throws IOException {
        List<SysDept> all = deptService.findAll(deptName,status);
        String fileName = SysDept.class.getSimpleName();
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), SysDept.class).sheet("excel表").doWrite(all);
    }

    @RequestMapping("/importExcel")
    public Integer importData(MultipartFile file) {
        try {
            //获取文件的输入流
            InputStream inputStream = file.getInputStream();
            List<SysDept> lst = EasyExcel.read(inputStream) //调用read方法
                    .registerReadListener(new UserListener(deptService))
                    .head(SysDept.class) //对应导入的实体类
                    .sheet(0) //导入数据的sheet页编号，0代表第一个sheet页，如果不填，则会导入所有sheet页的数据
                    .headRowNumber(1) //列表头行数，1代表列表头有1行，第二行开始为数据行
                    .doReadSync(); //开始读Excel，返回一个List<T>集合，继续后续入库操作
            //模拟导入数据库操作
            for (SysDept sysDept : lst) {
                String date = sysDept.getCreateTime();
                String form = String.format("%tF", date);
                System.out.println(form);
            }
            return 1;
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }
    }



}
